//
//  CustomUIPageControl.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/2.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "CustomUIPageControl.h"
#define dotW 18 // 圆点宽
#define dotH 1  // 圆点高
#define magrin 8    // 圆点间距
@implementation CustomUIPageControl
- (void)layoutSubviews
{
    [super layoutSubviews];
    //计算圆点间距
    CGFloat marginX = dotW + magrin;
    
    //计算整个pageControll的宽度
    CGFloat newW = (self.subviews.count - 1 ) * marginX;
    
    //设置新frame
    self.frame = CGRectMake(kScreenWidth/2-(newW + dotW)/2, self.frame.origin.y, newW + dotW, self.frame.size.height);
    
    //遍历subview,设置圆点frame
    for (int i=0; i<[self.subviews count]; i++) {
        UIImageView* dot = [self.subviews objectAtIndex:i];
        if ([dot.subviews count] == 0) {
            
           UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,18, 1)];
            
           [dot addSubview:view];
//            if (i == self.currentPage) {
//                view.image = IMAGE(@"蓝色_lion");
//            }else {
//                view.image = IMAGE(@"白色_lion");
//            }
        };
        UIImageView *imageView = dot.subviews[0];
        if (i == self.currentPage) {
            [dot setFrame:CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotH)];
             imageView.image = IMAGE(@"蓝色_lion");
        }else {
            [dot setFrame:CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotH)];
            imageView.image = IMAGE(@"白色_lion");
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//- (void) setCurrentPage:(NSInteger)page {
//
//    [super setCurrentPage:page];
//
//    for (NSUInteger i =0; i < [self.subviews count]; i++) {
//
//        UIView* dot = [self.subviews objectAtIndex:i];
//
//        [dot setFrame:CGRectMake(dot.frame.origin.x , dot.frame.origin.y,15, 1)];
//        dot.backgroundColor = kRedColor;
//        if ([dot.subviews count] == 0) {
//
////            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,50, 1)];
////
////            [dot addSubview:view];
//
//        };
//
//
//
////        UIImageView *imageView = dot.subviews[0];
////
////
////
//        if (i == page) {
//
//            imageView.image = IMAGE(@"蓝色_lion");
//
//        } else {
//
//            imageView.image = IMAGE(@"白色_lion");
//
//        }
////
//        dot.backgroundColor = kRedColor;
//
//    }
//
//}
@end
